﻿using SharpDX;
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Engine.FileFormats3D
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MoxMaterial
    {
        private int ambientColor;
        private Vector3 diffusColor;
        private string id;
        private int reflect;
        private int specularColor;
        private int specularColor2;
        private string tex1Name;
        private float TexAngle;
        private byte[] texFlags;
        private Vector2 TexOffset;
        private Vector2 TexScale;
        private float transparency;
        private int xDiffuse;
        private int xSpecular;

        public Vector3 DiffusColor
        {
            get { return diffusColor; }
            set { diffusColor = value; }
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Tex1Name
        {
            get { return tex1Name; }
            set { tex1Name = value; }
        }

        public static MoxMaterial FromStream(StreamReader sr, string id)
        {
            MoxMaterial material = new MoxMaterial(id.Substring(2, 4));
            string line;
            while ((line = sr.ReadLine()) != string.Empty && line != null)
            {
                string[] data = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                switch (data[0])
                {
                    case "Tex1Name":
                        material.tex1Name = data[1].Substring(1, data[1].Length - 2);
                        string extension = Path.GetExtension(material.tex1Name);
                        if (extension.ToUpper() == ".TGA")
                            material.tex1Name = material.tex1Name.Substring(0, material.tex1Name.Length - 3) + "ptx";
                        break;

                    case "Diffuse":
                        int r, g, b;
                        r = Int32.Parse(data[1].Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
                        g = Int32.Parse(data[1].Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
                        b = Int32.Parse(data[1].Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
                        material.diffusColor = new Vector3(r / 255.0f, g / 255.0f, b / 255.0f);
                        break;

                    default:
                        Console.WriteLine(data[0] + " not used atm.");
                        break;
                }
            }

            return material;
        }

        public MoxMaterial(string id)
        {
            this.id = id;
            this.diffusColor = new Vector3(1.0f, 0.0f, 1.0f);
            this.ambientColor = System.Drawing.Color.Gray.ToArgb();
            this.specularColor = System.Drawing.Color.Gray.ToArgb();
            this.reflect = System.Drawing.Color.Gray.ToArgb();
            this.specularColor2 = System.Drawing.Color.Gray.ToArgb();
            this.xDiffuse = System.Drawing.Color.Gray.ToArgb();
            this.xSpecular = System.Drawing.Color.Gray.ToArgb();
            this.transparency = 0.5f;
            this.tex1Name = string.Empty;
            this.texFlags = new byte[4] { 0x11, 0x00, 0x00, 0x00 };
            this.TexOffset = Vector2.Zero;
            this.TexScale = Vector2.Zero;
            this.TexAngle = 0.0f;
        }
    }
}